package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.Code;
import com.domain.Medical;
import com.domain.Result;
import com.service.IMedicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/medical")
public class MedicalController {
    @Autowired
    private IMedicalService iMedicalService;

    @GetMapping
    public Result getAll(){
        List<Medical> list = iMedicalService.list();
        if(list!=null){
            return new Result(list,"查询成功", Code.GET_OK);
        }else
        {
            return new Result(list,"查询失败", Code.GET_ERR);
        }
    }

    @GetMapping("/s/{medicalId}")
    public  Result getById(@PathVariable Integer medicalId){
        Medical medical = iMedicalService.getById(medicalId);
        if(medical!=null){
            return new Result(medical,"查询成功", Code.GET_OK);
        }else
        {
            return new Result("查询失败", Code.GET_ERR);
        }
    }

//    @PostMapping("/s")
//    public Result getByName(String name){
//        List<Medical> list = iMedicalService.getByName(name);
//        if(list!=null){
//            return new Result(list,"查询成功", Code.GET_OK);
//        }else
//        {
//            return new Result("查询失败", Code.GET_ERR);
//        }
//    }
@GetMapping("/{medicalPatientName}")
public  Result getByName(@PathVariable String medicalPatientName){
    QueryWrapper<Medical> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("medical_patient_name",medicalPatientName);
    List<Medical> list = iMedicalService.list(queryWrapper);
    if(list.isEmpty()){
        return new Result("查询失败", Code.GET_ERR);
    }else
    {
        return new Result(list,"查询成功", Code.GET_OK);
    }
}

    @PostMapping
    public Result add(@RequestBody Medical medical){
        boolean flag = iMedicalService.save(medical);
        if(flag==true){
            return new Result("添加成功", Code.SAVE_OK);
        }else
        {
            return new Result("添加失败", Code.SAVE_ERR);
        }
    }

    @DeleteMapping("/{medicalId}")
    public Result deleteById(@PathVariable Integer medicalId){
        boolean flag = iMedicalService.removeById(medicalId);
        if(flag==true){
            return new Result("删除成功", Code.DELETE_OK);
        }else
        {
            return new Result("删除失败", Code.DELETE_ERR);
        }
    }

    @PutMapping
    public Result update(@RequestBody Medical medical){
        boolean flag = iMedicalService.updateById(medical);
        if(flag==true){
            return new Result("修改成功", Code.UPDATE_OK);
        }else
        {
            return new Result("修改失败", Code.UPDATE_ERR);
        }
    }


}

